
Vue.component('form-approval', {
    data: function () {
        return {
            showLoading: false,
            listLoading: false,
            listFinished: true,
            showSearch: false,
            showDatePicker: false,
            showSimpleSelect: false,
            simpleColumns: [],
            currentDate: new Date(),
            pickerIndex: -1,
            searchKey: "",
            tabActive: 0,

            objName: '审批记录表',

            currentCol: "",
            paging: {
                pageSize: 30,
                pageIndex: 1,
                totalCount: 0
            },
            mainKey: "",
            columns: [],
            params: [],
            // searchFilterArray: [],
            resultArray: [],
            selectArray: [],
            filterStr: "",
            filterDefault: "1=1",
            summaryData: []
        }
    },
    props: {
        billquery:{
            type: String,
            default: '1=1'
        }
    },
    template: '<div class="approval-content">\
                <van-list v-model="listLoading" :finished="listFinished" @load="listOnLoad">\
                    <div class="approval-row" v-for="(row,index1) in resultArray">\
                        <div class="pd">\
                            <van-cell v-for="(col,index2) in columns" v-show="!col.isHidden && col.colDesc != \'失败退回节点\'" :title="col.colDesc" :value="getRowName(row,col)"></van-cell>\
                        </div>\
                    </div>\
                    <div v-if="!resultArray.length" style="color:#9e9e9e;text-align:center;padding:15px 0;font-size:13px;">暂无审批记录</div>\
                </van-list>\
                </div>',

    created: function () {
        var approvalVm = this;
        setTimeout(function(){
            approvalVm.getColumns();
        },200)
        


    },

    methods: {
        getRowName(row, col) {
            var approvalVm = this;
            if (col.colDataType == 'date') {
                return (row[col.colName] || '').split('T')[0];
            } else if (col.colDataType == 'datetime') {
                return (row[col.colName] || '').replace(/T/g, ' ').split('.')[0];
            } else if (col.colDataType == 'time') {
                return (row[col.colName] || '').split('T')[1].split('.')[0];
            } else {
                if (col.colType == 2) {
                    if (col.selDetail.type == 1) {
                        return approvalVm.showSelectTableDisplayValue(col.selDetail, row[col.colName])
                    } else if (col.selDetail.type == 3) {
                        return approvalVm.showSelectTableDisplayValueByAjax(col.selDetail, row[col.colName])
                    } else {
                        return row[col.colName];
                    }
                } else {
                    return row[col.colName];
                }

            }
        },
        getColumns() {
            var approvalVm = this;
            FLD.formConfigAjax('FormList/GetDataByName', {
                programId: localStorage.getItem("programId"),
                objName: approvalVm.objName
            }, function (data) {
                var filterCondition = data.ds[0]["filterCondition"] || '';
                var userInfo = FLD.getUserInfo();
                for (var key in userInfo) {
                    var value = "'" + userInfo[key] + "'";
                    filterCondition = eval("filterCondition.replace(/" + "@" + key + "/g,value)")
                }


                if (!filterCondition) {
                    filterCondition += approvalVm.billquery
                } else {
                    filterCondition += " and " + (approvalVm.billquery || " 1=1 ") + " ";
                }
                approvalVm.columns = JSON.parse(data.ds[0]["columnsConfig"]);

                for (var i in approvalVm.columns) {
                    if (approvalVm.columns[i].isOrder) {
                        approvalVm.columns[i].defaultQuerySort = approvalVm.columns[i].querySort
                    }
                    if (approvalVm.columns[i].isKey) {
                        approvalVm.mainKey = approvalVm.columns[i].colName;
                    }
                };
                approvalVm.params = {
                    isFormList: data.ds[0]["isFormList"],
                    physicalTableName: data.ds[0]["physicalTableName"],
                    isPaging: data.ds[0]["isPaging"],
                    detailName: data.ds[0]["detailName"],
                    filterCondition: filterCondition,
                    defaultOrder: data.ds[0]["defaultOrder"]
                }
                if (filterCondition) {
                    approvalVm.filterStr = filterCondition + " and " + approvalVm.filterDefault;
                } else {
                    approvalVm.filterStr = approvalVm.filterDefault;
                }
                approvalVm.refreshFilterCol();

                approvalVm.setLists();
            })

        },
        refreshFilterCol() {
            var approvalVm = this;
            for (var i = 0; i < approvalVm.columns.length; i++) {
                //下拉框数据加载
                if (approvalVm.columns[i].colType == 2 && approvalVm.columns[i].selDetail.type < 3) {
                    var sql = approvalVm.columns[i].selDetail.dataSource;
                    $.ajax({
                        url: "http://" + FLD.serverIP + "/SQLData/GetDataBySql",
                        async: false,
                        data: {
                            programId: localStorage.getItem("programId"),
                            sql: sql
                        },
                        type: "post",
                        dataType: 'json',
                        success: function (data) {
                            if (data.status == 200) {
                                approvalVm.$set(approvalVm.columns[i].selDetail, "sqlDataSource", data.data.ds);
                                approvalVm.$set(approvalVm.columns[i], 'filters', data.data.ds);
                            }
                        }
                    });
                } else if (approvalVm.columns[i].colType == 2 && approvalVm.columns[i].selDetail.type == 3) {
                    var valueList = [];
                    var sql = "select * from (" + approvalVm.columns[i].selDetail.dataSource + ")t";
                    if (valueList.length > 0) {
                        sql += " where " + approvalVm.columns[i].selDetail.hiddenValue + " in (" + valueList.join(',') + ") ";
                        $.ajax({
                            url: "http://" + FLD.serverIP + "/SQLData/GetDataBySql",
                            async: false,
                            data: {
                                programId: localStorage.getItem("programId"),
                                sql: sql
                            },
                            type: "post",
                            dataType: 'json',
                            success: function (data) {
                                if (data.status == 200) {
                                    approvalVm.$set(approvalVm.columns[i].selDetail, "sqlDataSource", data.data.ds);
                                    approvalVm.$set(approvalVm.columns[i], 'filters', []);
                                }
                            }
                        });
                    }
                    else {
                        approvalVm.$set(approvalVm.columns[i].selDetail, "sqlDataSource", []);
                        approvalVm.$set(approvalVm.columns[i], 'filters', []);
                    }
                }
            }
        },
        setLists() {
            var approvalVm = this;
            FLD.formConfigAjax('SQLData/GetDataByPhysicalTableNameByPaging', {
                programId: localStorage.getItem("programId"),
                tableName: approvalVm.params.physicalTableName,
                pageSize: approvalVm.paging.pageSize,
                isForm: approvalVm.params.isFormList,
                filterCondition: approvalVm.filterStr,
                defaultOrder: approvalVm.params.defaultOrder
            }, function (data) {
                approvalVm.listFinished = false;
                approvalVm.resultArray = data.ds;
                if (data.ds.length > 0) {
                    //绑定总长度
                    approvalVm.paging.totalCount = data.ds1[0]["@TotalCount"];
                }
            })

        },
        listOnLoad() {
            var approvalVm = this;
            approvalVm.paging.pageIndex++;

            var queryColumns = [];
            for (var i = 0; i < approvalVm.columns.length; i++) {
                queryColumns.push({
                    queryKey: approvalVm.columns[i].queryKey,
                    colName: approvalVm.columns[i].colName,
                    queryWhere: approvalVm.columns[i].colQueryWhere,
                    filter_selection: approvalVm.columns[i].filter_selection,
                    querySort: approvalVm.columns[i].querySort
                })
            }
            FLD.formConfigAjax('SQLData/GetDataByPhysicalTableNameByQueryPaging', {
                programId: localStorage.getItem("programId"),
                tableName: approvalVm.params.physicalTableName,
                columns: queryColumns,
                currentCol: approvalVm.currentCol,
                pageIndex: approvalVm.paging.pageIndex,
                pageSize: approvalVm.paging.pageSize,
                isForm: approvalVm.params.isFormList,
                filterCondition: approvalVm.filterStr,
                defaultOrder: approvalVm.params.defaultOrder
            }, function (data) {
                approvalVm.listLoading = false;
                approvalVm.resultArray = approvalVm.resultArray.concat(data.ds);

                if (data.ds.length > 0) {
                    //绑定总长度
                    approvalVm.paging.totalCount = data.ds1[0]["@TotalCount"];
                } else {
                    approvalVm.listFinished = true;
                }
            })


        },
        showSelectTableDisplayValue(db, selectValue) {
            var approvalVm = this;
            var dbList = db.sqlDataSource.filter(function (item) { return item[db.hiddenValue] == selectValue })
            if (dbList.length > 0) {
                return dbList[0][db.displayValue];
            }
            else {
                return "";
            }
        },
        //通过ajax获取显示值，增加性能查询
        showSelectTableDisplayValueByAjax(db, selectValue) {
            var approvalVm = this;
            if (db.chooseType == 1 || db.chooseType == 2) {
                try {
                    var dbList = db.sqlDataSource.filter(function (item) { return item[db.hiddenValue] == selectValue })
                    if (dbList.length > 0) {
                        return dbList[0][db.displayValue];
                    }
                    else {
                        return "";
                    }
                } catch (e) {
                    return "";
                }
            } else if (db.chooseType == 3) {
                try {
                    var dbList = db.sqlDataSource.filter(function (item) { return ("," + selectValue + ",").indexOf("," + item[db.hiddenValue] + ",") >= 0 })
                    if (dbList.length > 0) {
                        var resultArray = [];
                        for (var i = 0; i < dbList.length; i++) {
                            resultArray.push(dbList[i][db.displayValue]);
                        }
                        return resultArray.join(',');
                    }
                    else {
                        return "";
                    }
                } catch (e) {
                    return "";
                }
            }
            return "";
        },
    }

})